# class Solution:
#     def candy(self, ratings: List[int]) -> int:
#         candys=[1]
#         for i in range(1,len(ratings)):
#             if ratings[i]>ratings[i-1]:
#                 candys.append(candys[-1]+1)
#             elif ratings[i]==ratings[i-1]:
#                 candys.append(1)
#             else:
#                 candys.append(1)
#                 j=i-1
#                 while j>=0 and ratings[j]>ratings[j+1]:
#                     if candys[j]<=candys[j+1]:
#                         candys[j]=candys[j+1]+1
#                     j-=1
                
#         #print(candys)
#         return sum(candys)
#回溯爆炸，递减序列会超时

class Solution:
    def candy(self, ratings) -> int:
        seq=[]
        for i in range(len(ratings)-1):
            if ratings[i]<ratings[i+1]:
                seq.append(1)
            elif ratings[i]==ratings[i+1]:
                seq.append(0)
            else:
                seq.append(-1)
        candys=0
        i=0
        edge=1
        print(seq)
        n=len(seq)
        if n==0:
            return 1
        while i<n:
            print(candys)
            if seq[i]>0:
                pos=0
                temp=i
                while i<n and seq[i]>0:
                    pos+=1
                    i+=1
                candys+=sum(range(edge,edge+pos+1))
                if temp>0 and seq[temp-1]<0:
                    candys-=1
                edge+=pos
                continue
            if seq[i]==0:
                temp=i
                zero=0
                while i<n and seq[i]==0:
                    zero+=1
                    i+=1
                if zero>1:
                    candys+=zero-1
                if i==n:
                    candys+=1
                if temp==0:
                    candys+=1
                edge=1
                continue
            if seq[i]<0:
                neg=0
                temp=i
                while i<n and seq[i]<0:
                    neg+=1
                    i+=1
                if temp>0 and seq[temp-1]>0:
                    if neg>pos:
                        candys-=edge
                        candys+=sum(range(1,neg+2))
                    else:
                        candys+=sum(range(1,neg+1))
                else:
                    candys+=sum(range(1,neg+2))
                edge=1
        return candys

s=Solution()
print(s.candy(

[10,10,10,10,10,10]))
                
'''
执行用时：
84 ms
, 在所有 Python3 提交中击败了
54.08%
的用户
内存消耗：
16.8 MB
, 在所有 Python3 提交中击败了
8.68%
的用户
'''
